package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.oss.OssTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.UserTimes;
import com.itheima.domain.mongo.Voice;
import com.itheima.domain.vo.VoiceVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.VoiceService;

import jdk.nashorn.internal.objects.annotations.Where;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Component
public class VoiceManager {
    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    VoiceService voiceService;

    @Autowired
    OssTemplate ossTemplate;

    //发送语音
    public ResponseEntity sendVoice(MultipartFile soundFile) throws IOException {
        //上传到阿里云
        String url = null;
        if (null!=soundFile) {
            url = ossTemplate.upload(soundFile.getOriginalFilename(), soundFile.getInputStream());
            System.out.println(url);
        }

        //封装声音
        Voice voice = new Voice();
        voice.setSoundUrl(url);
        voice.setUserId(UserHolder.get().getId());
        voice.setCreated(System.currentTimeMillis());

        voiceService.sendVoice(voice);

        return ResponseEntity.ok(null);
    }

    //接收语音
    public ResponseEntity receiveVoice() {
        Long userId = UserHolder.get().getId();
        //获取剩余次数并进行判断
        UserTimes timesById = voiceService.findTimesById(userId);
        Integer times = timesById.getTimes();
        if (times<0) {
            VoiceVo voiceVo = new VoiceVo();
            voiceVo.setNickname("您今天剩余的次数不足，请明天再来");
            voiceVo.setRemainingTimes(times+1);
            return ResponseEntity.ok(voiceVo);
        }

        //判断语音是不是自己的，若是，再次查询
        /*Boolean flag=true;
        Voice voice=null;
        while (flag){
            voice = voiceService.receiveVoice();
            if (userId!=voice.getUserId()) {
                voice.setReceived(true);
                voiceService.sendVoice(voice);
                flag=false;
            }
        }*/
        List<Voice> voices = voiceService.receiveVoice();
        Voice voice=null;
        UserInfo userInfo = null;
        if (CollUtil.isNotEmpty(voices)) {
            for (Voice voice1 : voices) {
                if (userId!=voice1.getUserId()) {
                    voice1.setReceived(true);
                    voiceService.sendVoice(voice1);
                    voice=voice1;
                    //获取语音用户的userinfo
                    userInfo = userInfoService.findByUserId(voice.getUserId());
                    break;
                }
            }
        }else {
            voice=new Voice();
        }
        VoiceVo vo = new VoiceVo();
        //判断是否获取到语音，并给出提示，然后进行封装
        if (null!=voice) {
            vo.setId(Math.toIntExact(userId));
            vo.setAvatar(userInfo.getAvatar());
            vo.setNickname(userInfo.getNickname());
            vo.setGender(userInfo.getGender());
            vo.setAge(userInfo.getAge());
            vo.setSoundUrl(voice.getSoundUrl());
            vo.setRemainingTimes(times);
        }else {
            vo.setNickname("语音池里已经没有语音了，请稍后重试！");
            vo.setRemainingTimes(times);

        }
        return ResponseEntity.ok(vo);
    }
}
